package kr.ac.mju.mdc.mjuapp;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

/**
 * @author davidkim
 *
 */
public class MainImgBrdPagerAdapter extends PagerAdapter {
	private ArrayList<Bitmap> bmList;

	private Context context;
	private Handler handler;

	public MainImgBrdPagerAdapter(Context context, Handler handler) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.handler = handler;
		bmList = new ArrayList<Bitmap>();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return bmList.size();
	}

	@Override
	public boolean isViewFromObject(View view, Object obj) {
		// TODO Auto-generated method stub
		return view == obj;
	}

	@Override
	public Object instantiateItem(ViewGroup pager, int position) {
		// TODO Auto-generated method stub
		ImageView view = new ImageView(context);
		LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		view.setLayoutParams(params);
		view.setImageBitmap(bmList.get(position));
		view.setScaleType(ScaleType.CENTER_CROP);
		pager.addView(view);
		if (pager.getChildCount() == 1 && bmList.size() > 0)
			handler.sendEmptyMessage(MainActivity.IMAGE_BOARD_START_TIMER);
		return view;
	}

	@Override
	public void destroyItem(ViewGroup pager, int position, Object view) {
		// TODO Auto-generated method stub
		((ViewPager) pager).removeView((View) view);
	}

	/**
	 * @param bitmap
	 */
	public void addView(Bitmap bitmap) {
		bmList.add(bitmap);
		notifyDataSetChanged();
	}

	/**
	 * 
	 */
	public void clear() {
		bmList.clear();
		notifyDataSetChanged();
	}
}
/* end of file */
